package edu.hnu.cloudflight.android;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import edu.hnu.cloudflight.android.supply.DateBtnOnclick;
import edu.hnu.cloudflight.android.util.DateUtil;
import edu.hnu.cloudflight.android.util.ToastUtil;

public class FlightNumberQueryActivity extends Fragment {

	@SuppressWarnings("unused")
	private static final String TAG = FlightNumberQueryActivity.class.getSimpleName();
	
	private Button starDatetBtn;
	private Button endDateBtn;
	private TextView startDateText;
	private TextView endDateText;
	private EditText flightNumEdit;
	private Button query;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_flightnumberquery, container, false);
		
		return v;
	}

	@Override
	public void onResume() {
		super.onResume();
		starDatetBtn = (Button) getActivity().findViewById(R.id.flightnumberquery_btn_id_startdate);
		endDateBtn = (Button) getActivity().findViewById(R.id.flightnumberquery_btn_id_enddate);
		startDateText = (TextView) getActivity().findViewById(R.id.flightnumberquery_textview_id_startdate);
		endDateText = (TextView) getActivity().findViewById(R.id.flightnumberquery_textview_id_enddate);
		flightNumEdit = (EditText) getActivity().findViewById(R.id.edit_flightnumber);
		query = (Button) getActivity().findViewById(R.id.queryByFlightNum);
		
		String date = DateUtil.getNowDateToString(DateUtil.sdf1);
		startDateText.setText(date);
		endDateText.setText(date);
		starDatetBtn.setOnClickListener(new DateBtnOnclick(startDateText, getActivity()));
		endDateBtn.setOnClickListener(new DateBtnOnclick(endDateText,getActivity()));
		query.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String flightNum = flightNumEdit.getText().toString();
				if(flightNum==null||"".equals(flightNum)){
					ToastUtil.makeToastForShort(getActivity(), R.string.flightnumberquery_tos_flightnumnotnull);
					return;
				}
				String startDate = startDateText.getText().toString();
				String endDate = endDateText.getText().toString();
				Intent intent = new Intent(getActivity(),FlightNumberResultActivity.class);
				intent.setAction(FlightNumberResultActivity.ACTION_FROM_FLIGHTNUMQUERY);
				intent.putExtra("flightNum", flightNum);
				intent.putExtra("startDate", startDate);
				intent.putExtra("endDate", endDate);
				startActivity(intent);
				
			}
		});
	}

}
